/*
 * @Author: your name
 * @Date: 2022-04-24 20:04:31
 * @LastEditTime: 2022-04-24 23:01:43
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: /safe_transmission_platform/tests/test_config.cc
 */
#include "config.h"
#include <stdint.h>
#include <string>
#include <yaml-cpp/yaml.h>
#include <sstream>
#include <iostream>
#include <fstream>
#include <cstdio>

struct Person
{
    Person(int32_t ageVar, const std::string& numVar, const std::string& nameVar):age(ageVar),num(numVar),name(nameVar)  
    {    }
    Person(){}
    int32_t age;
    std::string num;
    std::string name;
};
template <>
class daniel::LexicalCast<Person,std::string>
{
public:
    std::string operator() (const Person& person)
    {
        YAML::Node root;
        YAML::Node childNode;
        childNode["age"]=person.age;
        childNode["num"]=person.num;
        childNode["name"]=person.name;
        root["person"]=childNode;
        std::stringstream ss;
        ss<<root;
        return ss.str();
    }
};
template <>
class daniel::LexicalCast<std::string,Person>
{
public:
    Person operator()(const std::string& str)
    {
        YAML::Node root=YAML::Load(str);
        Person retPerson;
        retPerson.age=root["age"].as<int>();
        retPerson.name=root["name"].as<std::string>();
        retPerson.num=root["num"].as<std::string>();
        return retPerson;
    }
};
//测试configVar的功能是否正常
void test_configVar()
{
    daniel::ConfigVar<Person>::ptr personConfig(new daniel::ConfigVar<Person>("person",Person(30,"123456","daniel"),"person"));
    /**/
    std::cout<<personConfig->toString()<<std::endl;
    std::fstream fs("/home/daniel/bin/safe_config/person.yml",std::ios::in);
    std::stringstream ss;
    std::string tmp;
    if (fs.is_open()){
        char buf[256];
        std::cout<<std::string(100,'*')<<std::endl;
        while (!fs.eof())
        {
            fs.read(buf,256);
            ss<<std::string(buf);
            printf("buf:%s\n",buf);
        }
        std::cout<<ss.str()<<std::endl;
        std::cout<<std::string(100,'*')<<std::endl;
    }
    fs.close();
    
    personConfig->fromString(ss.str());
    std::cout<<personConfig->toString()<<std::endl;
}
//测试配置管理类是否正常
void test_config()
{
    /*lookUp插入接口没问题*/
    daniel::ConfigMsg::GetInstance()->lookUp("person",Person(18,"5656","Alice"),"person");
    // daniel::ConfigMsg::GetInstance()->lookUp("port",8888,"port");
    daniel::ConfigMsg::GetInstance()->printAll();
    // daniel::ConfigMsg::GetInstance()->loadFromFile("/home/daniel/bin/safe_config/person.yml");
    daniel::ConfigMsg::GetInstance()->loadFromDir("/home/daniel/bin/safe_config");
    daniel::ConfigMsg::GetInstance()->printAll();
}


int main()
{
    test_configVar();
    std::cout<<std::string(100,'*')<<std::endl;
    test_config();
}